//查询已删除订单数据
function searchDeleteOrder(page) {
	$.ajax({
		type : "post",
		url : "/consume/order/searchDeleteOrder?page="+page,
		async : false,
		dataType : "json",
		success : function(data){
			$('#pageDelete').val(data.page);
			$('#pageCountDelete').val(data.pageCount);
			$('#delete').empty();
			//新增内容
			var tb_content = '';
			if(data.orderList.length > 0) {
				$.each(data.orderList,function(index,order){
					tb_content += '<ul class="cle">'
							   + '<li class="tb"><input type="checkbox" name="checkbox" class="checkbox_list" /></li>'
						 	   + '<li class="tp" data-id="'+order.orderId+'">'+order.orderNum+'</li>' 
							   + '<li class="tp">'+order.goodsName+'</li>'
							   + '<li class="tb">'+order.count+'</li>'
							   + '<li class="tb">'+checkIsFree(order.isFree,order.buyType)+'</li>'
							   + '<li class="tp">'+checkDelPayMoney(order.payMoney)+'</li>'
							   + '<li class="tb">'+checkOrderState(order.orderState,order.isDredge)+'</li>'
							   + '<li class="tp">'+toDate(order.occurTime,"yyyy-MM-dd hh:mm:ss")+'</li>'
							   + '<li class="tp"><a href="javascript:void(0)" onclick="restoreOrDel(this,'+order.orderId+',2)">还原</a> | <a href="javascript:void(0)" onclick="restoreOrDel(this,'+order.orderId+',1)">永久删除</a><br />'+checkProMeal(order.mealId,order.newProId)+'</li>'
							   + '</ul>';
				})
				
				tb_content += '<div class="page mt20" align="center">共'+data.totalRows+'条记录 '+data.page+'/'+data.pageCount+'页&nbsp;'+
							  '<a href="javascript:searchDeleteOrder(1)">&nbsp;首页</a> <a onclick="javascript:ajaxPageUpDelete('+data.page+')">上一页</a>&nbsp;'+
							  '<a onclick="javascript:ajaxPageDownDelete('+data.page+');">&nbsp;下一页</a>&nbsp;'+
							  '<a href="javascript:searchDeleteOrder('+data.pageCount+')" >尾页</a>&nbsp;&nbsp;第&nbsp;&nbsp;'+
							  '<select onchange="ajax2PageDelete()" id="currPageDelete" >';
				for(var i = 1; i <= data.pageCount; i++) {
					tb_content += '<option value="'+i+'" ';
					if(data.page == i) {
						tb_content += 'selected="selected"';
					}
					tb_content += '>'+i+'</option>';
				}
				tb_content += '</select>&nbsp;页</div>';
				
			}else {
				tb_content += '<p>没有符合条件的订单，请尝试其他搜索条件。</p>';
			}
			$('#delete').append(tb_content);
		}
	});
}

//判断是产品还是套餐
function checkProMeal(mealId,productId) {
	var bugAgain = '';
	if($.trim(productId) == null || $.trim(productId) == '') {
		bugAgain = '<a href="/mall/tcxq/'+mealId+'" class="detail">再次购买</a>';
	}else {
		bugAgain = '<a href="/mall/cpxq/'+productId+'" class="detail">再次购买</a>';
	}
	return bugAgain;
}

//判断支付金额
function checkDelPayMoney(payMoney) {
	payMoney = $.trim(payMoney);
	var str = '';
	if(payMoney == null || payMoney == '') {
		str = '0.00';
	}else {
		str = parseFloat(payMoney).toFixed(2);
	}
	return str;
}

//直接查询某页
function ajax2PageDelete() {
	searchDeleteOrder($('#currPageDelete').val());
}

//向上翻页
function ajaxPageUpDelete(page) {
	if(page == 1) {
		BSTYUN.dialog.warn("已经是第一页");
		return;
	}else {
		var num = parseInt(page) - 1;
		searchDeleteOrder(num);
	}
}

//向下翻页
function ajaxPageDownDelete(page) {
	if(page == $("#pageCountDelete").val()) {
		BSTYUN.dialog.warn("已经是最后一页");
		return;
	}else {
		var num = parseInt(page) + 1;
		searchDeleteOrder(num);
	}
}

//单个订单还原或删除
function restoreOrDel(that,orderIds,delete_state) {
	var deleteWarn = "",
		deleteFail = "";
	var $this = $(that);
	if(delete_state == 1) {
		deleteWarn = "确定要永久删除以下订单吗？";
		deleteFail = "永久删除失败，请重试！";
	}else {
		deleteWarn = "确定要还原以下订单吗？";
		deleteFail = "还原失败，请重试！";
	}

	BSTYUN.dialog.confirm(deleteWarn,function(){
		$.ajax({
			type : "post",
			url : "/consume/order/deleteOrder?orderIds="+orderIds,
			async : false,
			data : {
				deleteState : delete_state
			},
			dataType : "json",
			success : function(data){
				if(data) {
					$this.parents('ul').remove();
					BSTYUN.dialog.warn("操作成功");
				}else {
					BSTYUN.dialog.warn(deleteFail);
				}
			}
		});
	});
}